from Order import Order
import json

class ReaderFile:
    def read_file(self, path: str):
        pass


class TxtFileReader(ReaderFile):
    def read_file(self, path: str) -> list[Order]:
        """
            txt文件读取，并转化为order对象
        """
        textFile = open(path, 'r', encoding='UTF-8')
        orderList = []
        for fileContent in textFile:
            fileContent = fileContent.strip()
            contentList = fileContent.split(',')
            order = Order(contentList[0], contentList[1], contentList[2], contentList[3])
            orderList.append(order)
        textFile.close()
        return orderList


class JsonFileReader(ReaderFile):
    def read_file(self, path: str) -> list[Order]:
        """
        json文件的读取，并将读取的结果转为Order对象
        :param path:读取文件路径
        :return: orderList
        """
        jsonFile = open(path, 'r', encoding='UTF-8')
        orderList = []
        for file in jsonFile:
            file_content = json.loads(file)
            # file_content 为 （dict）字典类型，如何转为Order对象类型？
            order = Order(file_content['date'], file_content['order_id'], file_content['money'], file_content['province'])
            orderList.append(order)
        jsonFile.close()
        return orderList

if __name__ == '__main__':
    textFileReader = TxtFileReader()
    orderList = textFileReader.read_file('D:\\其他学习\\python学习\\python黑马课堂资料\\资料\\第13章资料\\2011年1月销售数据.txt')
    for order in orderList:
        print(order)

    # jsonFileReader = JsonFileReader()
    # orderList = jsonFileReader.read_file('D:\\其他学习\\python学习\\python黑马课堂资料\\资料\\第13章资料\\2011年2月销售数据JSON.txt')
    # for order in orderList:
    #     print(order)